package jmm;

import java.awt.event.WindowStateListener;
import java.util.concurrent.CountDownLatch;

/**
 * @author Brook
 * 演示重排序的现象，
 * 直到达到某个条件才停止，测试小概率事件
 */
public class OutOfOrderExecution {

    //private static int x = 0, y = 0;
    //private static int a = 0, b = 0;

    //使用volatile修饰可以禁止重排序
    private volatile static int x = 0, y = 0;
    private volatile static int a = 0, b = 0;

    public static void main(String[] args) throws InterruptedException {

        int i = 0;
        for (; ;) {
            i++;
            x=0;
            y=0;
            a=0;
            b=0;

        CountDownLatch latch = new CountDownLatch(1);
        Thread two = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    latch.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                b = 1;
                y = a;
            }
        });
        Thread one = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    latch.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                a = 1;
                x = b;
            }
        });

        one.start();
        two.start();
        latch.countDown();
        one.join();
        two.join();

        String result = "第"+i+"次(" + x + "," + y + ")";
        if(x == 0 && y == 0){
            System.out.println("x="+x+",y="+y);
            break;
        }else {
            System.out.println(result);
        }
        }
    }

}
